package com.moneywelcome.atomloanassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moneywelcome.atomloanassembly.dto.AmortizationDto;
import com.moneywelcome.atomloanassembly.exception.FindandupdateAmortizationExecption;
import com.moneywelcome.atomloanassembly.exception.UserLoanExecption;
import com.moneywelcome.atomloanassembly.mapper.AmortizationMapper;
import com.moneywelcome.atomloanassembly.mapper.UserLoanMapper;
import com.moneywelcome.atomloanassembly.model.Amortization;
import com.moneywelcome.atomloanassembly.model.UserLoan;
import com.moneywelcome.atomloanassembly.service.AmotizationService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Service
public class AmortizationServiceImpl extends ServiceImpl<AmortizationMapper, Amortization> implements AmotizationService {

    @Resource
    private UserLoanMapper userLoanMapper;

    @Override
    public AmortizationDto findandupdateAmortizationbyId(Integer userId) throws ParseException {
        QueryWrapper<Amortization> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        Amortization one = getOne(qw);
        if (one==null){
            throw new UserLoanExecption(10060,"查询对象为空");
        }
        QueryWrapper<UserLoan> qw1 = new QueryWrapper<UserLoan>();
        qw1.eq("user_id",userId);
        UserLoan userLoan = userLoanMapper.selectOne(qw1);
        //设置时间格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date loanTime = userLoan.getLoanTime();//贷款时间

        Date l = new Date();//获取当前系统时间
        String format5 = format.format(l);//获取的当前系统时间

        Date nextRepayTime = one.getRealRepayTime();
        String format3 = format.format(nextRepayTime);//获取最晚还款时间的时间戳
        Calendar b = Calendar.getInstance();
        b.setTime(nextRepayTime);
        b.add(Calendar.DATE,- 7);
        Date time1 = b.getTime();
        String format2 = format.format(time1);//获取出账时间，为前7天时间,出账时间

        int compareto = time1.compareTo(l);//判断出账时间先后
        int compareto2 = nextRepayTime.compareTo(l);//判断是否逾期

        if (compareto<0&compareto2>0){
           one.setOutaccountStatus("已出账");
           boolean b1 = updateById(one);
            if (!b1){
                throw new FindandupdateAmortizationExecption(10070,"修改出账状态失败");
            }
        }
        if (compareto>0){
            throw new FindandupdateAmortizationExecption(10071,"未出账");
        }
        //日期比较结束过后还款，还款后修改贷款分期信息
        System.out.println("当前期数为第"+one.getStageNumber()+"期");
        System.out.println("本期最晚还款时间为："+format3);
        System.out.println("本次出账时间为："+format2);
        System.out.println("出账状态为："+one.getOutaccountStatus());
        AmortizationDto dto = new AmortizationDto();
        BeanUtils.copyProperties(one,dto);
        return dto;
    }

    //还款后修改贷款分期信息,修改期数，修改还款时间
    //修改出账状态，最后一期修改还款金额
    @Override
    public AmortizationDto afterRepay(Integer userId) {
        //查询出该用户分期信息
        QueryWrapper<Amortization> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        Amortization one = getOne(qw);
        if (one==null){
            throw new UserLoanExecption(10060,"查询对象为空");
        }
        //查询出该用户贷款信息
        QueryWrapper<UserLoan> qw1 = new QueryWrapper<UserLoan>();
        qw1.eq("user_id",userId);
        UserLoan userLoan = userLoanMapper.selectOne(qw1);
        //对分期信息进行操作，基于修改，对用户对应的信息进行修改
        //不是最后一期的时候
        if (one.getStageNumber()<userLoan.getLoanStage()){//不是最后一期分期的时候
            one.setStageNumber(one.getStageNumber()+1);//设置下一期还款期数
            Calendar c = Calendar.getInstance();
            Date nextRepayTime = one.getRealRepayTime();
            c.setTime(nextRepayTime);
            c.add(Calendar.MONTH,+1);
            Date time = c.getTime();//这是修改后的下次还款时间，加一个月
            one.setRealRepayTime(time);//设置下一期的最晚还款时间
            one.setOutaccountStatus("未出账");//设置下一期的出账状态
            Date date = new Date();
            one.setRepayTime(date);//设置本次支付时间
            if (updateById(one)) {
                AmortizationDto am =new AmortizationDto();
                BeanUtils.copyProperties(one,am);
                return am;
            }
        }
        //最后一期的时候，只修改当前支付时间，清空下次还款时间
        if (one.getStageNumber()==userLoan.getLoanStage()){
            Date date = new Date();
            one.setRepayTime(date);
            one.setOutaccountStatus("已还清");
            one.setRealRepayTime(null);
            if (updateById(one)) {
                System.out.println("已还清");
                AmortizationDto amortizationDto = new AmortizationDto();
                BeanUtils.copyProperties(one,amortizationDto);
                return amortizationDto;
            }
        }
        return null;
    }



}
